#!/usr/bin/perl
#modifyId200712202327
#modify to show more information


use strict;
my $line;
my $numargs = @ARGV;
if($numargs < 4)
{
	die("ExSingleInfo.pl ReportFileName SiteNumber MutaionTypes[1457] outpath\n\n");
}

my $infile = $ARGV[0]; #iput file 
my $site = $ARGV[1]; # site number
my $mutType = $ARGV[2];#mutation type
my $outPath = $ARGV[3];#out put path
#modifyId200712202327
my $parslt = $infile;
$parslt =~s/report\.txt/parseRlt\.txt/;
my $mirRlt = $infile;
$mirRlt =~s/report\.txt/mirandaRlt\.txt/;


print $parslt." ".$mirRlt;

unless(open(INFILE, $infile))
{
	die("Can not open $infile!/n/n");
}

$outPath.=$site.".html";
unless(open(OUTFILE, ">$outPath"))
{
	die("can not create $site.html file");
}
###create header
print OUTFILE '
<html>
<header>
<title>Site '.$site.'</title>
</headr>
<body>
<center><h2><font face="Arial" color="blue">miGLE Locus Report:'.$site.'</font></h2></p>
Version 1.0 </center>
';
#####
unless(open(MIRRLT,$mirRlt))
{
	print OUTFILE "Can $ARGV[0] not $infile  cccccccc open $mirRlt";
}
my $mirRltPath = "";
while($line =<MIRRLT>)
{
	if($line=~/Query Filename\:/)
	{
		chomp($line);
		my @field = split(/\t+/, $line);
		$field[1] =~s/hsa-mature-miR\.fasta/miRBASE\.txt/;
		$mirRltPath = $field[1];
		#print OUTFILE $field[2];
		last;
	}
}
close();
###########
my $count =0;
my @list = split(//, $mutType);
my $microRNA = "";
my $oldmiR = "";
my $mirbaseUrl = "http://microrna.sanger.ac.uk/cgi-bin/sequences/mirna_entry.pl?acc=";
while($line = <INFILE>)
{
	chomp($line);
	
	if($line=~/^>/)
	{	
	my @field = split(/ /, $line);
	$microRNA = $field[1];
	$count++;
	
	if($count>1)
	{
		print OUTFILE "\n";
		print OUTFILE "</table>";
		print OUTFILE "\n";
		print OUTFILE "<p></p><p></p>";
	}
	
	next;
	}
	
	my @mysplit = split(/[#\|]/, $line);
	my $thisType = $mysplit[9];
	my $id = $mysplit[0];
	
	#If the id is the equal to the user clicked
	if($id == $site)
	{
		my $valid = -1;
		for(my $i=0; $i<=$#list; $i++)
		{
			if($thisType == $list[$i])
			{
				$valid = $thisType;
				last;
			}
		}
		
		if($valid > 0)
		{
		
			if($oldmiR !~/$microRNA/)
			{
				&getMirbaseInfo($microRNA, $mirbaseUrl, $mirRltPath);
				
				$oldmiR = $microRNA;
								
				&getOriginInfo($parslt, $microRNA);
			}
			
			#print OUTFILE $line."  <a href= \"\../../../help.html#$valid\" target=\"_blank\">?</a>"."</p>";
			
            #the following code is used to output the microRNA information.
			#print OUTFILE $line."</p>";
			&Line2Table($line);
		}
	}
}

####CREATE END OF FILE
print OUTFILE '
</body>
</html>
';
close($infile);
#modifyId200801231632
sub Line2Table()
{
	chomp($line);
	my ($in) = @_;
	print OUTFILE "\n";
	print OUTFILE "<tr bgcolor=\"#FFFFCC\">";
	print OUTFILE "\n";
	
	my @field = split(/\#/, $line);
	print OUTFILE "<th scope=\"row\"><div align=\"left\">".$field[0]."</div></th>";
	print OUTFILE "\n";
	my @fd = split(/[-][\>]/, $field[1]);
	print OUTFILE "<td><div align=\"left\">".$fd[0]."</div></td>";
	print OUTFILE "\n";
    
	my @sp = split(/\|/, $fd[1]);
	for(my $i = 0; $i<= $#sp; $i=$i+2)
	{
		print OUTFILE "<td><div align=\"left\">".$sp[$i]."</div></td>";
		print OUTFILE "\n";
	}
	print OUTFILE "  </tr>";
}


#modifyId200712202327
sub getOriginInfo()
{
	my ($resultfile, $curMiR) = @_;
	unless(open(PARSERESULT, $resultfile))
	{
		die("Can not open $resultfile !/n/n");
	}

	my $line = "";
	while($line=<PARSERESULT>)
	{
		if($line=~/$curMiR/)
		{
			while($line=<PARSERESULT>)
			{	
			
				if($line=~/TargetEnd/)
				{
					#out put the head of the table
					print OUTFILE "\n";
					print OUTFILE "<table width=\"836\" border=\"0\" cellspacing=\"0\">";
					print OUTFILE "\n";
					last;
				}
				else
				{
					chomp($line);
					my $trans = &targInfTrans($line);
					print OUTFILE $trans."</p>";
				}
			}
			last;
		}
	}
	#print the head of the table
	print OUTFILE "\n";
	print OUTFILE '  <tr>
    <th colspan="7" scope="col"><div align="center">
	<strong>Mutant in microRNA target region</strong>
	<a href= "../../../help.html" target="_blank\">?</a><p></p>
	</div></th>
  </tr>
    <tr bgcolor="#66FF99">
    <th width="111" scope="row"><div align="left"><em><strong>Location</strong></em></div></th>
    <td width="74"><div align="left"><em><strong>Original base</strong></em></div></td>
    <td width="62"><div align="left"><em><strong>Mutant</strong></em></div></td>
    <td width="118"><div align="left"><em><strong>Target on Refseq </strong></em></div></td>
    <td width="157"><div align="left"><em><strong>Gain Target after Mutation </strong></em></div></td>
    <td width="175"><div align="left"><em><strong>Loss Target after Muation </strong></em></div></td>
    <td width="125"><div align="left"><em><strong>funcType</strong></em></div></td>
  </tr>
  ';
	print OUTFILE "\n";
	
	close(PARSERESULT);
}

sub getMirbaseInfo()
{
	my($miR, $url, $mirbase) = @_;
	unless(open(MIRBASE, $mirbase))
	{
		print OUTFILE  "can not open $mirbase\n";
		print OUTFILE "<hr><b><font size=5>".$microRNA."</font></b></p><strong>Refseq</strong></p>";
		return;
	}
	my $line = ""; 
	print OUTFILE "<hr><b><font size=5>$microRNA</font></b>";
	my @sp = split(/\-/, $miR);
	$miR = $sp[0]."-".$sp[1]."-".$sp[2];
	$miR= uc($miR);
	
	while($line = <MIRBASE>)
	{
		chomp($line);
		$line=uc($line);
		chomp($miR);
		if($line=~/$miR/i)
		{
			my @field = split(/\"/, $line);
			my @spl = split(/\-/, $field[3]);
			my $target = "";
			if($#spl >2)
			{
				$target = $spl[0]."-".$spl[1]."-".$spl[2];
			}
			else
			{
				$target = $field[3];
			}
		
			if($target eq $miR)
			{
				print OUTFILE "   <a href= $url$field[1] target=\"_blank\">$field[1]</a>";
			}
		}
	}
	print OUTFILE "</p><strong>Refseq</strong></p>";
	close(MIRBASE);
}

sub targInfTrans()
{
	my ($in) = @_;
	my $toinfo = "Target Region @";
	$in=~s/TargetAt//;
	$in = $toinfo.$in;
	return $in;
}

